#include "Engine.h"
#include "Core.h"
#include "Utils\SingletonPattern.h"
#include "Process.h"
#include "Core.h"
#include "Utils\Defines.h"
#include "XML\XMLTreeNode.h"
#include <Windows.h>
#include "GraphicsManager.h"

#include <string>

CEngine::CEngine(void)
{
}


CEngine::~CEngine(void)
{
}

void
CEngine::Init(CProcess *process, std::string xmlfile, HWND hWnd)
{
	m_process = process;
	
	CXMLTreeNode newFile;
	if (newFile.LoadFile(xmlfile.c_str()))
	{
		CXMLTreeNode m = newFile["Config"];
		if (m.Exists())
		{
			int count = m.GetNumChildren();
			for (int i = 0; i < count; ++i)
			{
				std::string name = m(i).GetName();
				if (name == "ScreenResolution")
				{
					m_width = m(i).GetIntProperty("width");
					m_height = m(i).GetIntProperty("height");
				}
				if (name == "Rendermode")
					m_fullscreen = m(i).GetBoolProperty("fullscreenMode");
			}
		}
	}

	m_core = new CCore();
	m_core->Init(hWnd, m_fullscreen, m_width, m_height);
}

void
CEngine::DeInit()
{
	m_process->DeInit();
	CHECKED_DELETE(m_process);
	m_core->DeInit();
	CHECKED_DELETE(m_core);
}

void
CEngine::Update()
{
	m_core->Update();
	m_process->Update();
}

void
CEngine::Render()
{
	m_core->GetgraphicsManager()->BeginRendering();
	m_core->GetgraphicsManager()->SetupMatrices(m_process->GetActiveCamara());
	m_core->Render();
	m_process->Render();
	m_core->GetgraphicsManager()->EndRendering();
}